package com.example.a001.part06;

import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.example.a001.R;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Locale;

public class TableLayou02Activity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_table_layou02);

        TableLayout tableLayout = findViewById(R.id.calendarTable);
        initWeekHeader(tableLayout);
        fillCalendar(tableLayout);
    }

    private void initWeekHeader(TableLayout tableLayout) {
        String[] weekNames = {"日", "一", "二", "三", "四", "五", "六"};
        TableRow row = new TableRow(this);
        for (String name : weekNames) {
            Button button = new Button(this);
            button.setText(name);
            button.setGravity(Gravity.CENTER);
            row.addView(button);
        }
        tableLayout.addView(row);
    }

    private void fillCalendar(TableLayout tableLayout) {
        Calendar calendar = Calendar.getInstance();
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH);

        calendar.set(year, month, 1);
        int firstDayOfMonth = calendar.get(Calendar.DAY_OF_WEEK) - 1;
        int daysInMonth = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);

        TableRow currentRow = null;
        int dayCount = 0;

        // 处理当月前的空白
        for (int i = 0; i < firstDayOfMonth; i++) {
            if (dayCount % 7 == 0) {
                currentRow = new TableRow(this);
            }
            Button emptyBtn = new Button(this);
            emptyBtn.setText("");
            currentRow.addView(emptyBtn);
            dayCount++;
        }

        // 填充当月日期
        for (int day = 1; day <= daysInMonth; day++) {
            if (dayCount % 7 == 0) {
                currentRow = new TableRow(this);
            }
            Button dateBtn = new Button(this);
            dateBtn.setText(String.valueOf(day));
            dateBtn.setGravity(Gravity.CENTER);
            final int finalDay = day;
            dateBtn.setOnClickListener(v -> showDateDetail(finalDay));
            currentRow.addView(dateBtn);
            dayCount++;

            // 每行填满7个按钮后添加到TableLayout
            if (dayCount % 7 == 0) {
                tableLayout.addView(currentRow);
            }
        }

        // 处理最后一行不满7个按钮的情况
        if (dayCount % 7 != 0) {
            tableLayout.addView(currentRow);
        }
    }

    private void showDateDetail(int day) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DAY_OF_MONTH, day);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd", Locale.CHINA);
        String dateStr = sdf.format(calendar.getTime());
        Toast.makeText(this, "选中日期：" + dateStr, Toast.LENGTH_SHORT).show();
    }
}